package com.xzy.servlet;

import com.xzy.domain.Book;
import com.xzy.domain.BookPage;
import com.xzy.service.IBookService;
import com.xzy.service.impl.BookServiceImpl;
import com.xzy.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/manager/bookservlet")
public class BookServlet extends BaseServlet{

    private IBookService service = new BookServiceImpl();

    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        Book book = WebUtils.copyParametToBean(new Book(), req.getParameterMap());
        service.addBook(book);
        resp.sendRedirect("bookservlet?type=bookPage&pageOn="+req.getParameter("pageOn"));

    }
    protected void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Book> books = service.quaryBooks();
        req.setAttribute("bookList",books);
        req.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(req,resp);
    }

    protected void update (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Book book = WebUtils.copyParametToBean(new Book(), req.getParameterMap());
        service.updateBook(book);
        resp.sendRedirect("bookservlet?type=bookPage&pageOn="+req.getParameter("pageOn"));
    }

    /**
     * 新增和修改都先进入此方法分流
     * 若请求参数中含有id参数则证明为修改,反之为添加
     * 修改操作设置编辑页面回显信息,添加不设置
     * 同时修改编辑页面中表单提交到哪一个方法的隐藏域
     * 	此为该隐藏域:
     * 	<input type="hidden" name="type" value=${requestScope.Mode}>
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected  void toEdit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        if(req.getParameter("id")!=null){
            int id = Integer.valueOf(req.getParameter("id"));
            Book book = service.quaryByid(id);
            req.setAttribute("quarybook",book);
            //设置隐藏域值为update 表示编辑页面提交后type值为updata(进入updata方法)
            req.setAttribute("Mode","update");
            req.setAttribute("pageOn",req.getParameter("pageOn"));
            req.getRequestDispatcher("/pages/manager/book_edit.jsp").forward(req,resp);
        }else{
            req.setAttribute("Mode","add");
            req.setAttribute("pageOn",req.getParameter("pageOn"));
            req.getRequestDispatcher("/pages/manager/book_edit.jsp").forward(req,resp);
        }
    }
    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String strid = req.getParameter("id");
        Integer id = Integer.valueOf(strid);
        service.DeleteByid(id);

        resp.sendRedirect("bookservlet?type=bookPage&pageOn="+req.getParameter("pageOn"));

    }

    /**
     * 分页实现原理:
     * Servlet从浏览器获得两个参数: 当前页码pageOn和每页显示数Pagesize
     * 调用service中业务方法 getPage(pageOn, pageSize);
     * 该方法利用参数计算出begin参数(当前页数据在数据库中的起始位置)公式:(pageon-1)*pageSize
     *
     * serviec在调用dao中方法quaryBooksByPage(Integer bagin, Integer size) 查询数据库中从begin条记录开始的size条记录
     * 将查询到的list数据封装到page页面中交给前端输出
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void bookPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        //接收当前页码和每页显示数两个参数


        Integer pageOn;
        Integer pageSize;
        pageSize = (Integer) WebUtils.getValue(req.getParameter("pageSize"), BookPage.PAGE_SIZE);
        pageOn = (Integer) WebUtils.getValue(req.getParameter("pageOn"), 1);

        BookPage<Book> page = service.getPage(pageOn, pageSize);
        page.setUrl("manager/bookservlet?type=bookPage");
        req.setAttribute("page",page);
        req.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(req,resp);

    }
}
